<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2018 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Group as groupModel;
use app\common\model\GroupCategories as categoriesModel;

/**
 * 星球管理控制器
 * @package app\admin\controller
 */
class Categories extends Admin
{

    /**
     * 星球分类列表
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $map = [];
            $keyword = $this->request->param('keyword');
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            if ($keyword) {
                $map['name'] = ['like', '%' . $keyword . '%'];
            }
            $map['status'] = 1;
            $data['data'] = categoriesModel::where($map)->page($page)->limit($limit)->order('sort asc')->select();
            $data['count'] = categoriesModel::where($map)->count('id');
            $data['code'] = 0;
            return json($data);
        }

        return $this->fetch();
    }

    /**
     * 创建分类
     */
    public function createCategories()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'GroupCategories.create');
            if($result !== true) {
                return $this->error($result);
            }

            unset($data['id']);
            if (!categoriesModel::create($data)) {
                return $this->error('添加失败！');
            }
            return $this->success('添加成功。');
        }

        return $this->fetch('form');
    }


    /**
     * 查看星球
     * @author jyxxpy
     * @param  groupId
     * @return mixed
     */
    public function edit($id = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'GroupCategories.update');
            if ($result !== true) {
                return $this->error($result);
            }

            if (!categoriesModel::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }

        $row = categoriesModel::where('id', $id)->find()->toArray();
        $this->assign('data_info', $row);
        return $this->fetch('form');
    }

}
